
var app = getApp()
var getAppGlobalData = require('../../templates/copyright/copyright.js');
Page({
  data: {
    staffInfo: {},
    StaffCard: {},
    globalData: {},
    notRead: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数 
    var that = this;
    getAppGlobalData.getAppGlobalData(that);
    wx.hideShareMenu(); 
    that.getStaffCard();
  },
  onReady: function () {
    // console.log("页面渲染完成")
  },
  onShow: function () {
    // 页面显示 
    var that = this;
    that.getStaffInfo();
    that.getStaffUnread();
  },
  onHide: function () {
    // console.log("页面隐藏")
  },
  onUnload: function () {
    // console.log("页面关闭")
  },
  onPullDownRefresh: function () {
    // console.log("监听用户下拉动作")
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom: function () {
    // console.log("监听页面上拉触底") 
  },
  onPageScroll: function (e) {
    //console.log("监听页面滚动", e);
  },
  onShareAppMessage: function (res) {
    // console.log("用户点击右上角分_享")  
    var that = this;
    var tmpData = that.data.staffInfo;
    if (res.from === 'button') {
      // console.log("来自页面内转发按钮");
    }
    else {
      // console.log("来自右上角转发菜单");
    }
    
    var title = [];
      
    if(app.globalData.company.name){
      title.push(app.globalData.company.name)
    }
    if(tmpData.info.job){
      title.push(tmpData.info.job)
    }
    if(tmpData.info.name){
      title.push(tmpData.info.name) 
    }

    let tmpTitle = '';
    for(let i in title){
      if(!title[i]){
        title.splice(i,1)
      }
      tmpTitle += (title[i] + '-')
    }
    tmpTitle = tmpTitle.slice(0,-1)
    console.log(title,tmpTitle,"title00000000///////")

    return {
      title: tmpTitle,
      path: '/longbing_card/pages/index/index?to_uid=' + wx.getStorageSync("userid") + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toCard',
      imageUrl: that.data.StaffCard.avatar,
      success: function (res) {
        console.log("转发成功", res)
        that.toShareRecord();
      },
      fail: function (res) {
        console.log('转发失败');
      }
    };
  }, 
  getStaffInfo: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/Staff',
      'cachetime': '30',
      'method': 'POST',
      'data': {
      },
      success: function (res) {
        // console.log("entry/wxapp/Staff ==>", res)
        if (!res.data.errno) {
          that.setData({
            staffInfo: res.data.data
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    })
  },
  getStaffCard: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/StaffCard',
      'cachetime': '30',
      'method': 'POST',
      'data': {
      },
      success: function (res) {
        // console.log("entry/wxapp/Staff ==>", res)
        if (!res.data.errno) {
          that.setData({
            StaffCard: res.data.data.count
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    })
  },
  getStaffUnread: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/Unread',
      'cachetime': '30',
      'method': 'POST',
      'data': {
      },
      success: function (res) {
        // console.log("entry/wxapp/Unread ==>", res)
        if (!res.data.errno) {
          that.setData({
            notRead: res.data.data.count
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    })
  },
  formSubmit: function (e) {
    var that = this;
    var formId = e.detail.formId;
    var status = e.detail.target.dataset.status;
    that.toSaveFormIds(formId);
    if (status == 'toCardIndex') {
      console.log("名片预览")
      wx.navigateTo({
        url: '/longbing_card/pages/index/index?to_uid=' + wx.getStorageSync("userid") + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toCard'
      })
    } else if (status == 'toEwm') {
      console.log("名片码")
      wx.navigateTo({
        url: '/longbing_card/pages/card/share/share?to_uid=' + wx.getStorageSync("userid") + '&form_id=' + wx.getStorageSync("userid")
      })
    } else if (status == 'toCode') {
      console.log("自定义码")
      wx.navigateTo({
        url: '/longbing_card/staff/spread/news/news?status=code'
      })
    } else if (status == 'toHome') {
      console.log("返回首页")
      wx.navigateTo({
        url: '/longbing_card/pages/index/index?to_uid=' + wx.getStorageSync("userid") + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toCard'
      })
    }
  },
  toSaveFormIds: function (formId) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/formid',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        formId: formId
      },
      success: function (res) {
        // console.log("entry/wxapp/formid ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    })
  },
  toJump: function (e) {
    var that = this;
    var status = e.currentTarget.dataset.status;
    if (status == 'toEdit') {
      console.log("编辑个人信息")
      wx.navigateTo({
        url: '/longbing_card/staff/mine/editInfo/editInfo'
      })
    } else if (status == 'toChat') {
      console.log("私信我的")
      wx.switchTab({
        url: '/longbing_card/staff/message/message'
      })
    } else if (status == 'toCourse') {
      console.log("使用教程")
    } else if (status == 'toOpinion') {
      console.log("意见反馈")
    }
  }
})